package com.zyasdk.util;

import android.app.DownloadManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.os.Build.VERSION;
import android.os.Handler.Callback;

import com.zyasdk.Zreceiver;
import com.zyasdk.Zservice;

public class ServiceUtil {
	public static ServiceUtil su;

	public static ServiceUtil getModel() {
		if (su == null)
			su = new ServiceUtil();
		return su;
	}

	public static Zreceiver reveiver = new Zreceiver();
	private static ZThread t = null;
	private Zservice service;
	private Handler handller = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			// 1秒定时发送
			sendMyBroadcast("com.zyasdk.zservice.timetask");
			return true;
		}
	});

	public void init(Zservice service) {
		ZYALogUtils.e("service init");
		this.service = service;
		if (t == null) {
			t = new ZThread();
		} else {
			t.isLoop = false;
			t.notifyStop();
			t = new ZThread();
		}
		if (!t.isLoop) {
			t.isLoop = true;
		}
		t.start();
		registerReceiver();
		sendMyBroadcast("com.zyasdk.zservice.startcommand");
	}
	
	public void registerReceiver(){
		try {
			// 每次重启服务都注册一遍广播，防止广播被杀掉
			IntentFilter intentFilter = new IntentFilter();
			intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);// 下载完成
			intentFilter.addAction("android.intent.action.BOOT_COMPLETED");// 设备开机
			intentFilter.addAction("android.intent.action.USER_PRESENT");// 用户解锁
			intentFilter.addAction("com.zyasdk.zservice.destroy");// 内部服务销毁
			intentFilter.addAction("com.zyasdk.zservice.create");// 内部服务创建
			intentFilter.addAction("com.zyasdk.zservice.timetask");// 定时发送
			intentFilter.addAction("com.zyasdk.zservice.startcommand");// 服务激活
			intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 网络状态变更
			intentFilter.addAction(Intent.ACTION_TIME_TICK);// 时间改变
			intentFilter.addAction("android.intent.action.HEADSET_PLUG");// 时间改变
			intentFilter.addAction("android.intent.action.WALLPAPER_CHANGED");// 壁纸变更
			intentFilter
					.addAction("android.intent.action.NETWORK_TICKLE_RECEIVED");// 接收到一个Tickle通知
			intentFilter.addAction("android.intent.action.BATTERY_OK");// 电量充足
			intentFilter.addAction("android.intent.action.POWER_CONNECTED");// 插上外部电源
			intentFilter
					.addAction("android.intent.action.CONFIGURATION_CHANGED");// 设置改变
			intentFilter.addAction("android.intent.action.CAMERA_BUTTON");// 设置改变
			service.registerReceiver(reveiver, intentFilter);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void sendMyBroadcast(String action) {
		Intent startIntent = new Intent();
		startIntent.setAction(action);
		if (VERSION.SDK_INT > 12) {
			startIntent.setFlags(0x00000020);
			// startIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
		}
		service.sendBroadcast(startIntent);
	}

	class ZThread extends Thread {
		private boolean isLoop = true;

		public ZThread() {
			super(null, null, "zya-thread", 0);
			setPriority(Thread.MAX_PRIORITY);
			setDaemon(true);
		}

		@Override
		public void run() {
			while (isLoop) {
				synchronized (this) {
					try {
						this.wait(1000);
						handller.sendEmptyMessage(0);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			isLoop = false;
		}

		public void notifyStop() {
			synchronized (this) {
				this.notify();
			}
		}
	}
}
